package cn.tgq.ioc.overview.dependency.injection;

import cn.tgq.ioc.overview.repository.UserRepository;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.env.Environment;

public class DependencyInjectionDemo {

    public static void main(String[] args) {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("classpath:/META-INF/dependency-injection-context.xml");
        // 依赖来源1：自定义Bean
        UserRepository userRepository = beanFactory.getBean("userRepository", UserRepository.class);
        // System.out.println(userRepository.getUsers());
        // 依赖来源2：依赖注入【内建依赖】（1）
        System.out.println(userRepository.getBeanFactory());
        // System.out.println(userRepository.getBeanFactory() == beanFactory);
        // 依赖查找（2）
        // System.out.println(beanFactory.getBean(BeanFactory.class));
        // （1）与（2）的结果不同，说明依赖查找和依赖注入的Bean的来源不同

        // ObjectFactory<User> userFactory = userRepository.getUserObjectFactory();
        // System.out.println(userFactory.getObject());

        // 依赖来源3：容器内建Bean
        Environment environment = beanFactory.getBean(Environment.class);
        System.out.println(environment);

        ObjectFactory<ApplicationContext> userFactory = userRepository.getObjectFactory();
        System.out.println("=================================================================");
        // ======================================================

    }

    private static void whoIsIoCContainer(BeanFactory beanFactory, UserRepository userRepository) {

    }

}
